package com.tanhua.sso.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.sso.config.HuanXinConfig;
import com.tanhua.sso.vo.HuanXinUser;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.*;


@Service
public class HuanxinService {

    @Autowired
    private HuanXinConfig huanXinConfig;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HuanXinTokenService huanXinTokenService;

    //定义Mapper

    private static  final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 注册欢心用户
     * @param userId 自己的id
     * @return
     */
    public boolean register(Long userId){
        String url = this.huanXinConfig.getUrl()
                +this.huanXinConfig.getOrgName()+"/"
                +this.huanXinConfig.getAppName()+"/users";

        //获取环信token
        String token = this.huanXinTokenService.getToken();
        //请求头信息  注意导包信息 springframework
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type","application/json");
        httpHeaders.add("Authorization","Bearer"+token);

        List<HuanXinUser> huanXinUsers = new ArrayList<>();
        huanXinUsers.add(new HuanXinUser(userId.toString(), DigestUtils.md5Hex(userId + "_itcast_tanhua")));

        try {
            HttpEntity<String> httpEntity = new HttpEntity(MAPPER.writeValueAsString(huanXinUsers),httpHeaders);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            //等于200注册成功
            return responseEntity.getStatusCodeValue() == 200;
        } catch (Exception e) {
            e.printStackTrace();
        }


        //注册失败返回false
        return false;
    }

    /**
     * 添加好友
     * @param userId
     * @param friendId
     * @return
     */

    public boolean contactUsers(Long userId,Long friendId){
        String targetUrl = this.huanXinConfig.getUrl()
                +this.huanXinConfig.getOrgName()+"/"
                +this.huanXinConfig.getAppName()+"/users/"
                +userId+"/contacts/users/"+friendId;
        try {
            String token = this.huanXinTokenService.getToken();
            //请求头
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type","application/json");
            headers.add("Authorization","Bearer"+token);

            HttpEntity<String> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(targetUrl,httpEntity,String.class);
            return  responseEntity.getStatusCodeValue()==200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //添加失败
        return false;
    }

    public Boolean sendMsg(String target, String msg, String type) {
        try {
            String targetUrl = this.huanXinConfig.getUrl()+this.huanXinConfig.getOrgName()+"/"
                    +this.huanXinConfig.getAppName()+"/messages";
            String token = this.huanXinTokenService.getToken();
            //请求头
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type","application/json");
            headers.add("Authorization","Bearer"+token);

            Map<String ,Object> requestParam = new HashMap<>();
            requestParam.put("target_type","users");
            //设置数组
            requestParam.put("target", Arrays.asList(target));
            requestParam.put("msg",msg);


            Map<String,Object> msgParam= new HashMap<>();
            msgParam.put("type",type);
            msgParam.put("msg",msg);


            //表示消息发送者;无此字段Server会默认设置为“from”:“admin”，有from字段但值为空串(“”)时请求失败
//            msgMap.put("from", type);

            //headers  请求头查询
            HttpEntity<String> httpEntity = new HttpEntity<>(MAPPER.writeValueAsString(requestParam),headers);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(targetUrl, httpEntity, String.class);
            return responseEntity.getStatusCodeValue()==200;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}
